class CoursewareController {

    static allowedMethods = [save: "POST", update: "POST"]
    def authenticateService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        Categorys categorys = Categorys.get(params.id)
        if (categorys) {
            [coursewareInstanceList: Courseware.findAllByCategory(categorys , params), 
                    coursewareInstanceTotal: Courseware.countByCategory(categorys) , 
                    categorys : categorys] 
        }else{
            [coursewareInstanceList: [], coursewareInstanceTotal: 0]
        }
    }

    def create = {
        def coursewareInstance = new Courseware()
        log.info "Courseware.create hit:${coursewareInstance.hit} value: ${coursewareInstance.value} ${coursewareInstance.createTime}"

        coursewareInstance.properties = params
        def categorys = Categorys.get(params.category.id) 
        return [coursewareInstance: coursewareInstance , categorys : categorys]
    }

    def save = {
        def coursewareInstance = new Courseware(params)
        coursewareInstance.school = session.school
        coursewareInstance.editor = authenticateService?.userDomain()

        if (coursewareInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'courseware.label', default: 'Courseware'), coursewareInstance])}"
            redirect(action: "list" , id : coursewareInstance?.category?.id )
        }
        else {
            render(view: "create", model: [coursewareInstance: coursewareInstance])
        }
    }

    def show = {
        def coursewareInstance = Courseware.get(params.id)
        if (!coursewareInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'courseware.label', default: 'Courseware'), params.id])}"
            redirect(action: "list")
        }
        else {
            coursewareInstance.hit++
            coursewareInstance.save()
            [coursewareInstance: coursewareInstance]
        }
    }

    def edit = {
        def coursewareInstance = Courseware.get(params.id)
        if (!coursewareInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'courseware.label', default: 'Courseware'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [coursewareInstance: coursewareInstance ]
        }
    }

    def update = {
        def coursewareInstance = Courseware.get(params.id)
        if (coursewareInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (coursewareInstance.version > version) {
                    
                    coursewareInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'courseware.label', default: 'Courseware')] as Object[], "Another user has updated this Courseware while you were editing")
                    render(view: "edit", model: [coursewareInstance: coursewareInstance])
                    return
                }
            }
            coursewareInstance.properties = params
            coursewareInstance.lastTime = new Date()
            coursewareInstance.lastEditor =  authenticateService?.userDomain()

            if (!coursewareInstance.hasErrors() && coursewareInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'courseware.label', default: 'Courseware'), coursewareInstance])}"
                redirect(action: "list", id : coursewareInstance?.category?.id )
            }
            else {
                render(view: "edit", model: [coursewareInstance: coursewareInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'courseware.label', default: 'Courseware'), params.id])}"
            redirect(action: "list" , controller: 'coursewareMgr')
        }
    }

    def delete = {
        def coursewareInstance = Courseware.get(params.id)
        if (coursewareInstance) {
            try {
                coursewareInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'courseware.label', default: 'Courseware'), params.id])}"
                redirect(action: "list" , id : coursewareInstance?.category?.id)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'courseware.label', default: 'Courseware'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'courseware.label', default: 'Courseware'), params.id])}"
            redirect(action: "list")
        }
    }
}
